#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Insert <!--Start/End of placeholder /path/"placeholder name"-->
comments before and after the placeholder. Very useful for debugging.
"""

import xml.dom.minidom

def execute_minidom(server, document, parameters, current_node=None):
	if not current_node:
		current_node = document

	# Don't modify a collection collection we're walking through
	# Cache the nodes we need to process
	nodes_to_process = [ ]

	# Look for nodes looking like <pp:placeholder name="...">
	for n in current_node.childNodes:
		if n.nodeType == document.ELEMENT_NODE:
			if n.localName == 'placeholder' and n.prefix == 'pp':
				nodes_to_process.append(n)
			
			execute_minidom(server, document, parameters, n)

	# Now insert comments
	for n in nodes_to_process:
		# Before the placeholder
		current_node.insertBefore(document.createComment(
			u'Start of placeholder %s/"%s"' % (
				n.getAttribute('url-base'),
				n.getAttribute('name'))),
			n)

		# After the placeholder
		current_node.insertBefore(document.createComment(
			u'End of placeholder %s/"%s"' % (
				n.getAttribute('url-base'),
				n.getAttribute('name'))),
			n.nextSibling)
